package cn.xiongxf.org.controller;

import cn.xiongxf.basic.annotation.PreAuthorize;
import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.PageList;
import cn.xiongxf.org.domain.Department;
import cn.xiongxf.org.query.DepartmentQuery;
import cn.xiongxf.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @GetMapping    等价于：@RequestMapping(method = {RequestMethod.GET})
 * @PostMapping   等价于：@RequestMapping(method = {RequestMethod.POST})
 * @PathVariable  从路径中获取变量
 * @RequestBody   从请求体【请求数据包中】中获取数据
 */

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门API",description = "部门的CRUD操作") // 描述Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有") // 描述方法
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：获取部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树") // 描述方法
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Department findById(@PathVariable Long id){
        return departmentService.findById(id);
    }

    /**
     * 接口：分页查询
     * @param departmentQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Department> queryPageList(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPageList(departmentQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult del(@PathVariable Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "部门批量删除",sn = "departmnet:patchDelete")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：新增/修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增/修改")
    @PreAuthorize(name = "部门保存",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

}
